/*
 * @Author: yao
 * @Date: 2024-11-12 08:59:32
 * @LastEditTime: 2025-02-18 17:18:29
 * @LastEditors: yao
 * @Description:
 * @FilePath: \jdev-web-el\src\stores\modules\user.ts
 */
import { defineStore } from "pinia";
import type { LoginParams } from "..";
import { getUserResources, login } from "@/api/user";
import { getToken, setToken } from "@/utils/utils";
import Storage from "@/utils/storage";
import config from "@/config/index";
import router from "@/router";
import { generatorDynamicRouter } from "@/router/generator-router";
import { ElMessage } from "element-plus";
import { getAppMenus } from "@/api/menu";
import constantRoutes from "@/router/constant-routes";

const parseRoleModule = () => {
  let modulesList = Storage.get(config.RESOS_KEY);
  let modules: any = [];
  modulesList.forEach((item: any) => {
    if (item.systemId == config.SYSTEM.ID && item.moduleId) {
      modules.push(item.moduleId);
    }
  });
  return modules;
};



export const useUserStore = defineStore("user", {
  state: () => {
    return {
      userInfo: Storage.get("USERINFO"),
      token: getToken,
      roles: Storage.get("ROLES"),
      auths: [],
      modules: Storage.get("MODULES"),
      menus: [],
      favMenu: Storage.get("FAVMENU") || []
    };
  },
  actions: {
    async RESET_USER_STATE() {
      this.$reset();
    },
    async loginHandler(params: LoginParams) {
      const res = await login(params);
      if (!res || res.code!==200 || !res.data) {
        ElMessage.error(res.message || "登录失败");
        return res;
      }
      setToken(res.data);
      this.token = res.data;
      return res.data;
    },
    async getResources() {
      getUserResources().then(res => {
        if (res.code === 200) {
          Storage.set('USERINFO', res.data);
        }
      });
      const res = await getAppMenus();
      if (res.code === 200) {
        Storage.set(config.RESOS_KEY, res.data);
        const mData: any = await this.GenerateRoutes(
          Storage.get(config.RESOS_KEY)
        );
        mData.forEach((route: any) => {
          router.addRoute(route);
        });
        return res.data;
      }
    },
    async GenerateRoutes(data: any) {
      return new Promise((resolve, reject) => {
        generatorDynamicRouter(data)
          .then((routers: any) => {
            resolve(routers);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    setRoutes(routes: any) {
      this.menus = routes;
      Storage.set("MENUS", routes);
    },
    addFav(menu: any) {
      if (this.favMenu.length % 2 !== 0 || this.favMenu.length === 0) {
        this.favMenu.unshift(menu)
      } else {
        this.favMenu.push(menu)
      }
      Storage.set("FAVMENU", this.favMenu);
    },
    removeFav(id) {
      let index = this.favMenu.findIndex(item => item.id === id);
      if (index !== -1) {
        this.favMenu.splice(index, 1);
      }
      // index = this.favMenu.findIndex(item => item.id === -3);
      // if (index !== -1) {
      //   this.favMenu.splice(index, 1);
      // }
      Storage.set("FAVMENU", this.favMenu);
    }
  },
});
